# main.py
import sys
import threading
from xhs.db_queue import load_tasks_loop
import time
from xhs.device_manager import DeviceManager
from xhs import ui
import traceback
from xhs.my_log import log
import PyQt6.QtCore

if __name__ == "__main__":
    try:
        log("[系统启动]")
        log("=" * 40)
        # 启动线程：查询数据放到队列中
        task_thread = threading.Thread(target=load_tasks_loop, daemon=True)
        task_thread.start()

        # ui界面
        app = ui.QApplication(sys.argv)
        window = ui.DeviceManagerUI()
        window.show()

        # 启动设备扫描与调度
        manager = DeviceManager(window)
        manager_thread = threading.Thread(target=manager.scan_loop, daemon=True)
        manager_thread.start()

        sys.exit(app.exec())

    except Exception as e:
        print("发生错误：")
        print(str(e))
        print("\n详细错误信息：")
        traceback.print_exc()  # 打印完整的堆栈跟踪
    # finally:
        # 等待用户输入，防止窗口自动关闭
        # input("\n按回车键退出...")



